package com.usian.wemedia.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import com.usian.apis.wemedia.WmUserControllerApi;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.media.pojos.WmUser;
import com.usian.user.threadlocal.WmThreadLocalUtils;
import com.usian.wemedia.service.WmUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/api/v1/user")
public class WmUserController implements WmUserControllerApi {

    @Autowired
    private WmUserService wmUserService;
    /**
     * 确保添加之前没有相同的用户
     * 幂等型
     * @param name
     * @return
     */
    @Override
    @GetMapping("/findByName/{name}")
    public WmUser findByName(@PathVariable("name") String name) {
        WmUser wmUser1= WmThreadLocalUtils.getUser();
        System.out.println(wmUser1.getId());
        List<WmUser> list=wmUserService.list(new LambdaQueryWrapper<WmUser>().eq(WmUser::getName,name));
        //判断如果数据库中有这个用户名，则直接返回第一个  之后在进行不添加即可
        for (WmUser wmUser : list) {
            return wmUser;
        }
        return null;
    }

    /**
     * 根据id查询自媒体用户
     * @param id
     * @return
     */
    @GetMapping("/findOne/{id}")
    @Override
    public WmUser findWmUserById(Long id) {
        return wmUserService.getById(id);
    }

    /**
     * 添加自媒体人信息
     * @param wmUser
     * @return
     */
    @Override
    @PostMapping("/save")
    public ResponseResult save(@RequestBody WmUser wmUser) {
        wmUserService.save(wmUser);
        return null;
    }


}
